from datetime import datetime
from db.crud.industry import get_industry_tree

def transform_data(input_data):
    result = []
    industry_map = {}
    
    for item in input_data:
        id1, name1, sub_id, sub_name, info = item
        
        if id1 not in industry_map:
            industry_map[id1] = {
                "id": id1,
                "name": name1,
                "data": []
            }
            result.append(industry_map[id1])
        
        industry_map[id1]["data"].append({
            "id": sub_id,
            "name": sub_name,
            "info": info
        })
    
    return result

class IndustryService:
    async def get_tree(self):
        query_data = await get_industry_tree()
        result = transform_data(query_data)
        # print(result)
        return result